#!/bin/bash

# Copyright 2017-2021 NXP
#
# SPDX-License-Identifier: BSD-3-Clause


build_distro_rfs_poky() {
    [ $username = root ] && fbprint_w "Please build poky as non-root account" && exit
    mkdir -p $PKGDIR/yocto
    [ -d $PKGDIR/yocto/poky/meta-poky/recipes-core ] || \
    git clone $repo_yocto_poky_url $PKGDIR/yocto/poky -b $repo_yocto_poky_tag
    [ -d $PKGDIR/yocto/meta-openembedded/meta-oe/recipes-core ] || \
    git clone $repo_yocto_oe_url $PKGDIR/yocto/meta-openembedded -b $repo_yocto_oe_branch

    pokyrfs=rootfs_${DISTRIB_VERSION}_poky_${DISTROVARIANT}_${DESTARCH}
    . $PKGDIR/yocto/poky/oe-init-build-env $FBOUTDIR/rfs/$pokyrfs
    $FBDIR/configs/poky/reconfig.sh fixcfg
    cp -f $FBDIR/configs/poky/local_${DESTARCH}_${DISTROVARIANT}.conf $FBOUTDIR/rfs/$pokyrfs/conf/local.conf
    if [ `grep -c "meta-oe" $FBOUTDIR/rfs/$pokyrfs/conf/bblayers.conf` -eq '0' ]; then
	cd $PKGDIR/yocto/meta-openembedded
	bitbake-layers add-layer meta-oe
	bitbake-layers add-layer meta-python
	bitbake-layers add-layer meta-networking
	bitbake-layers add-layer meta-multimedia
	cd -
    fi
    export LANG=en_US.UTF-8
    [ $DESTARCH = arm64 ] && export MACHINE=qemuarm64 || export MACHINE=qemuarm
    # bitbake -c cleansstate $pokyimgtype
    bitbake $pokyimgtype
    [ $DESTARCH = arm64 -a $DISTROVARIANT = devel ] && $FBDIR/configs/poky/reconfig.sh fixlib $RFSDIR/usr/lib/libc.so
    [ -L $FBOUTDIR/rfs/$pokyrfs/target ] || ln -s $RFSDIR $FBOUTDIR/rfs/$pokyrfs/target

    # setup app components (restool, aiop_tool, tsntool, fmc, etc) in poky userland
    echo Building and installing app components for $DISTROTYPE $DISTROVARIANT
    bld apps -r poky:$DISTROVARIANT -a ${DESTARCH} -p $SOCFAMILY
    if [ $DISTROVARIANT = tiny ]; then
	mkdir -p $RFSDIR/usr/bin $RFSDIR/usr/local/bin $RFSDIR/usr/lib
	[ -f $DESTDIR/usr/local/bin/restool ] && sudo cp -f $DESTDIR/usr/local/bin/{ls-*,restool} $RFSDIR/usr/bin
	[ -f $DESTDIR/usr/bin/aiop_tool ] && sudo cp -f $DESTDIR/usr/bin/aiop_tool $RFSDIR/usr/bin
	[ -f $DESTDIR/lib/libfm-arm.a ] && sudo cp -f $DESTDIR/lib/libfm-arm.a $RFSDIR/lib
	[ -f $DESTDIR/usr/local/bin/fmc ] && sudo cp -f $DESTDIR/usr/local/bin/fmc $RFSDIR/usr/bin && \
	sudo cp -Prf --preserve=mode,timestamps $DESTDIR/etc/fmc $RFSDIR/etc && sudo cp -f $DESTDIR/usr/local/lib/*/libfmc.a $RFSDIR/usr/lib
	[ -f $DESTDIR/usr/local/bin/tsntool ] && sudo cp -f $DESTDIR/usr/local/bin/tsntool $RFSDIR/usr/bin && \
	sudo cp -f $DESTDIR/usr/lib/libtsn.so $RFSDIR/usr/lib
	sudo mkdir -p $RFSDIR/usr/local/bin
	[ -f $DESTDIR/usr/local/bin/ccsr ] && sudo cp -f $DESTDIR/usr/local/bin/ccsr $RFSDIR/usr/local/bin/
	sudo rm -rf $RFSDIR/usr/include/* $RFSDIR/usr/lib/aarch64-poky-linux $RFSDIR/usr/share/libtool $RFSDIR/usr/share/aclocal
	sudo echo TinyLinux | sudo tee $RFSDIR/etc/hostname 1>/dev/null
    elif [ $DISTROVARIANT = devel ]; then
	sudo cp -Prf --preserve=mode,timestamps $DESTDIR/* $RFSDIR/
	sudo mkdir -p $RFSDIR/lib/systemd/system
	sudo cp -f $FBDIR/src/system/boot.mount $RFSDIR/lib/systemd/system
	sudo echo DevelLinux | sudo tee $RFSDIR/etc/hostname 1>/dev/null
    fi
    distroinfo="$DISTRIB_NAME $DISTROVARIANT ${DISTRIB_VERSION: -4} (based on Yocto $repo_yocto_poky_tag)"
    echo $distroinfo | sudo tee $RFSDIR/etc/issue 1>/dev/null
    echo $distroinfo | sudo tee $RFSDIR/etc/buildinfo 1>/dev/null
    distrostamp="Build: `date +'%Y-%m-%d %H:%M:%S'`"
    echo "$distrostamp" | sudo tee -a $RFSDIR/etc/buildinfo 1>/dev/null
    sudo cp -f $FBDIR/tools/flex-installer $RFSDIR/usr/bin
    $FBDIR/configs/poky/reconfig.sh fixsys $RFSDIR
    cd $RFSDIR && sudo chown -h -R 0:0 * && \
    sudo find . | sudo cpio --quiet -o -H newc | gzip > $FBOUTDIR/images/$pokyrfs.cpio.gz && fbprint_d $FBOUTDIR/images/$pokyrfs.cpio.gz
    cp -f $pokytmpdir/deploy/images/$MACHINE/${pokyimgtype}-${MACHINE}.rootfs.manifest $FBOUTDIR/rfs/$pokyrfs/$pokyrfs.packages.manifest

    bld packrfs -r poky:$DISTROVARIANT -a $DESTARCH -p $SOCFAMILY
    ls -goh $FBOUTDIR/images/$pokyrfs*
}
